/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.core.parser;

import org.eclipse.dltk.ast.ASTNode;
import org.eclipse.dltk.ast.PositionInformation;
import org.eclipse.dltk.ast.declarations.TypeDeclaration;
import org.eclipse.dltk.ast.expressions.Expression;
import org.eclipse.dltk.ast.statements.Statement;
import org.eclipse.dltk.compiler.ISourceElementRequestor;
import org.eclipse.dltk.compiler.SourceElementRequestVisitor;

import com.funkyroach.use.core.ast.USFuncDeclaration;
import com.funkyroach.use.core.ast.USVariableDeclaration;

public class USSourceElementRequestor extends SourceElementRequestVisitor {

	public USSourceElementRequestor(ISourceElementRequestor requesor) {
		super(requesor);
	}

	public boolean visit(ASTNode expression) throws Exception {
		return true;
	}
		
	public boolean visit(Statement statement) throws Exception {
		if (statement instanceof USVariableDeclaration) {
			visitVariableDeclaration((USVariableDeclaration) statement);
		}
		if (statement instanceof USFuncDeclaration) {
			visitFuncDeclaration((USFuncDeclaration) statement);
		}
		return super.visit(statement);
	}

	private void visitVariableDeclaration(USVariableDeclaration varDecl)
			throws Exception {
		ISourceElementRequestor.FieldInfo info = new ISourceElementRequestor.FieldInfo();
		PositionInformation pos = varDecl.getPositionInformation();

		info.modifiers = varDecl.getModifiers();
		info.name = varDecl.getName();
		info.nameSourceEnd = pos.nameEnd - 1;
		info.nameSourceStart = pos.nameStart;
		info.declarationStart = pos.sourceStart;
		this.fRequestor.enterField(info);
		this.fRequestor.exitField(pos.sourceEnd);
	}

	private void visitFuncDeclaration(USFuncDeclaration funcDecl) {
		ISourceElementRequestor.MethodInfo info = new ISourceElementRequestor.MethodInfo();

		PositionInformation pos = funcDecl.getPositionInformation();

		info.modifiers = funcDecl.getModifiers();
		info.name = funcDecl.getName();
		info.nameSourceEnd = pos.nameEnd - 1;
		info.nameSourceStart = pos.nameStart;
		info.declarationStart = pos.sourceStart;
		this.fRequestor.enterMethod(info);
		this.fRequestor.exitModule(pos.sourceEnd);
	}

	public boolean visit(Expression expression) throws Exception {
		// System.out.println("Visit expression: " + expression);
		return super.visit(expression);
	}

	public boolean visit(TypeDeclaration type) throws Exception {
		System.out.println("Visit type: " + type);
		return super.visit(type);
	}

}
